package com.kkb.robotcustomerservice.controller;


import com.kkb.robotcustomerservice.entities.Chat;
import com.kkb.robotcustomerservice.entities.JsonResult;
import com.kkb.robotcustomerservice.entities.Questions;
import com.kkb.robotcustomerservice.service.ChatService;
import com.kkb.robotcustomerservice.service.QuestionsService;
import com.kkb.robotcustomerservice.util.SendTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@CrossOrigin
public class SendMessageController {

    @Autowired
    private QuestionsService questionsService;
    @Autowired
    private ChatService chatService;

    @Autowired
    private Questions questions;

    @RequestMapping(value = "/chat/sendmessage/{id}/{type}/{question}/{sender}",method = RequestMethod.POST)
    public JsonResult sendMessage(@PathVariable("id")Integer id,
                                 @PathVariable("type")Integer type,
                                 @PathVariable("question")String question,
                                 @PathVariable("sender")Integer sender) {
        SendTime time = new SendTime();
        Chat chat = new Chat();
        JsonResult<Questions> result = new JsonResult<>();
        if (type==0) {
            Questions byId = questionsService.findById(id);
            String questionName = byId.getQuestionName();
            chat.setChatInfo(questionName);
            chat.setUserId(1);
            chat.setSender(sender);
            chat.setSendTime(time.sendTime());
            chatService.add(chat);

            String answer1 = byId.getAnswer();
            questions.setAnswer(answer1);
            result.setData(byId);
            result.setCode(0);
            if (answer1.isEmpty()){
                result.setMsg("数据库找不到");
            }else{
                result.setMsg("成功");
            }
            result.setSuccess(true);

            Chat chat1 = new Chat();
            chat1.setChatInfo(answer1);
            chat1.setSender(0);
            chat.setUserId(1);
            chat1.setSendTime(time.sendTime());
            chatService.add(chat1);
            return result;
        }else if (type==1){

            chat.setChatInfo(question);
            chat.setUserId(1);
            chat.setSender(sender);
            chat.setSendTime(time.sendTime());
            chatService.add(chat);


            String answer = "不好意思，我还没有学会这个问题，您可以换个问题问我";
            questions.setAnswer(answer);
            result.setData(questions);
            result.setCode(0);
            if (answer.isEmpty()){
                result.setMsg("数据库找不到");
            }
            result.setSuccess(true);

            Chat chat1 = new Chat();
            chat1.setChatInfo(answer);
            chat1.setSender(0);
            chat.setUserId(1);
            chat.setSendTime(time.sendTime());
            chatService.add(chat1);
            return result;
        }else {
            return result;
        }
    }
}
